# -*- coding: utf-8 -*-

import records
import pymysql
pymysql.install_as_MySQLdb()

db = records.Database('mysql://root:root@localhost/sanic?charset=utf8')
# db = records.Database('sqlite:///db.db')
conn = db.get_connection()

# 查询所有
# rows = conn.query("select * from person")
# rs = rows.all(as_dict=True)
# print(rs)

# for row in rows:
#     row = row.as_dict()
#     print(row)


# whrere 查询
where = {
    'id': '1',
}
rows = db.query(
    "select * from person where id=:id ",
    **where)
print(rows.dataset)

# 事务
# conn = db.get_connection()
# tx = conn.transaction()
# try:
#     conn.query("INSERT INTO usertest(userid, username) values(5, 'Peggi')")
#     conn.query("INSERT INTO usertest(userid, username) values(4, 'Alice')")
#     tx.commit()
# except Exception as ex:
#     print("*" * 10, "Error Occured", "*" * 10)
#     print(str(ex))
#     tx.rollback()
# finally:
#     conn.close()

# 使用说明
# https://www.cnblogs.com/wagyuze/p/11398484.html
# https://blog.csdn.net/stone0823/article/details/102558414